﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Weazel.Badger.Vhdl
{
  public class PortMap
  {
    private string name;
    private string entity;

    private int count = 0;

    private List<string> leftSideSignalNames =
      new List<string>();

    private List<string> rightSideSignalNames =
      new List<string>();

    public PortMap(string name, string entity)
    {
      this.name = name;
      this.entity = entity;
    }

    public void Add(string left, string right)
    {
      if (leftSideSignalNames.Contains(left))
        throw new Exception("left side signal '" + left + "' already in map");

			// Duplication of signalnames on the right hand side is wierd but ok

      leftSideSignalNames.Add(left);
      rightSideSignalNames.Add(right);

      count++;
    }

    public void Write(System.CodeDom.Compiler.IndentedTextWriter writer)
    {
      writer.WriteLine(string.Format("{0} : {1} port map (", name, entity));
      writer.Indent++;

      if (count == 0)
      {
        writer.WriteLine(" -- empty");
      }
      else
      {
        // for all but the last one..
        for (int i = 0; i < count - 1; i++)
          writer.WriteLine(string.Format("{0} => {1},", leftSideSignalNames[i], rightSideSignalNames[i]));

        writer.WriteLine(
          string.Format(
            "{0} => {1}", 
            leftSideSignalNames[count - 1], 
            rightSideSignalNames[count - 1]
          )
        );
      }

      writer.Indent--;
      writer.WriteLine(");");
    }
  }
}
